# encoding=utf8
import random
import time
import re
from selenium.webdriver import Chrome
from selenium.webdriver import ChromeOptions
import requests
# from random import random
# # from fake_useragent import UserAgent  # 获取随机useragent
# from concurrent import futures
# import json
# import sys
from lxml import etree
# from urllib.parse import urlencode
# from requests.cookies import RequestsCookieJar


# 日志文档
def writelogtxt(msg):
    try:
        with open('log.txt', 'a+', encoding='utf-8') as f:
            f.write(msg + '\n')
    except:
        pass

# 代理池随机调取函数
def get_proxy():
    return requests.get("http://127.0.0.1:5010/get/").content.decode('utf8')


# 简单的写入文档操作,把成功的词语铣刀match.txt里边
def writetxt(msg):
    try:
        with open('符合SEO的词语.txt', 'a+', encoding='utf-8') as f:
            f.write(msg + '\n')
    except Exception as e:
        print(e)
        print("写入1出错")
        pass

 # 简单的写入文档操作,遇到验证码关闭连接，然后这个词等于没有匹配，存到一个地方
def writeNopasstxt(msg):
    try:
        with open('遇到验证码需要重新跑的词语.txt', 'a+', encoding='utf-8') as f:
            f.write(msg + '\n')
    except:
        print("写入2出错")
        pass
#读取txt文档的关键词
def readtxt():
    wordlist = []
    for line in open("keywords.txt", encoding='utf-8'):
        wordlist.append(line.strip())
    print(wordlist)
    return wordlist

def etreeParse(content):
    try:
        ehtml = etree.HTML(content)
        searchlist = ehtml.xpath('//*[@id="sogou_vr_11002301_box_0"]/div/div[2]/p[1]/a/text()')
        writetxt(searchlist[0])
        return searchlist[0]
    except:
        pass

#匹配目标公众号
def matchGZH(content, keyword):
    abc = fr'<em><!--red_beg-->{keyword}<!--red_end--></em></a>'
    print(content)
    myvalue = re.search(abc,content,re.IGNORECASE)
    print(myvalue)
    if myvalue == None:
        # 丢弃
        # print(f'公众号没找到{keyword}')
        return False
    else:
        #匹配到公众号
        # print(f'公众号-------{keyword}')
        return True
#匹配目标微信
def matchwx(content, keyword):
    #进来的微信词语获得一下对应的公众号名，有名字的匹配下公众号
    try:
        wx_gzh = etreeParse(content)
        getresult = matchGZH(content, wx_gzh)
        if getresult: writetxt(wx_gzh)
    except Exception as e:
        print(e)
        pass
    abc = fr'<p class="info">微信号：<label name="em_weixinhao">{keyword}</label>'
    myvalue = re.search(abc, content, re.IGNORECASE)
    print(myvalue)
    if myvalue == None:
        # 丢弃
        # print(f'丢弃{keyword}')
        return False
    else:
        #匹配到微信号
        # print(f'微信号-------{keyword}')
        return True
#查询关键词，如果有就存到可用list
def matchkeyword(html,keyword):
    if matchwx(html, keyword) or matchGZH(html,keyword):
        print(f"{keyword}存入txt文档ok")
        writetxt(keyword)
    else:
        print(f"废弃词语{keyword}")
#通过关键词获取html
def gethtml(keyword,driver):
    timelist = [3,4,5,6,7,8,9,10,11,12,13,14,15]
    timenum = random.choice(timelist)
    time.sleep(timenum)
    print(f'Runtime--{keyword}开始')
    # header 伪装浏览器设置（下面的二选一，看哪个合适）
    # ua = UserAgent(use_cache_server=False)
    # ua = UserAgent(verify_ssl=False)
    # headers = {
    #     'User-Agent': ua.random,
    #     'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3',
    #     'Accept-Encoding': 'gzip, deflate, br',
    #     'Content-Type': 'application/x-www-form-urlencoded',
    #     'Accept-Language': 'zh-CN,zh;q=0.9',
    #     'Cache-Control': 'max-age=0',
    #     'Host': 'weixin.sogou.com',
    #     'Connection': 'keep-alive'
    # }

    # session = requests.session()
    # session.headers = headers
    # proxy = get_proxy()
    # print(f'proxy-----------{proxy}')
    # if proxy == "":
    #     print("代理列表为空，不进行请求，退出session")
    #     session.close()
    # html = session.get(
    #     url=f'https://weixin.sogou.com/weixin?type=1&query={keyword}&ie=utf8&s_from=input&_sug_=n&_sug_type_=')
    #     # url=f'https://weixin.sogou.com/weixin?type=1&query={keyword}&ie=utf8&s_from=input&_sug_=n&_sug_type_=',proxies={"https": "http://{}".format(proxy)})
    # html.encoding = 'utf-8'
    # print(html.text)
    # print(html.url)
    # print(html.headers)
    # session.headers.update(html.headers)
    # print(session.headers)
    # session.close()
    # try:
    #     f = requests.get(html.url)
    #
    #     print(f.content)
    #     # print(Snuid)
    #     print('f的header是')
    #     print(f.headers)
    # except Exception as e:
    #     print(e)
    #     Snuid = ""
    #     print("dsfsaffs111111111111")
    #-----------------------------------------------------------
    #记录到log文档
    nowtimt = time.strftime('%Y-%m-%d %H:%M:%S')
    writelogtxt(f"['{keyword}','{nowtimt}']")
    url=f'https://weixin.sogou.com/weixin?type=1&query={keyword}&ie=utf8&s_from=input&_sug_=n&_sug_type_='
    # url=f'https://weixin.sogou.com'
    SNUIDlist =[]
    driver.set_page_load_timeout(10)

    try:
        driver.get(url=url)
    except Exception as e:
        print(e)
        print("10秒钟超时")
        driver.quit()
        return True,True  #True--下一次重新创建实例, True--循环这个词语


    # SNUID = driver.get_cookies()[5]["value"]
    # if SNUID not in SNUIDlist:
    #     SNUIDlist.append(SNUID)
    html = driver.page_source
    # print(html)
    # print(SNUIDlist)
    serach = re.search('请输入图中的验证码', html)
    if serach != None:  #如果查询到有该字符，说明有验证码
    # 搜狗限制网页，等待输入验证码
    #     print('搜狗限制网页，等待验证码输入成功后开启')
    #     # print(content)
    #     SUsername = input("请确认验证码已成功开启，按任意字符回车继续")
        print(f'----X----X----X----X----Runtime--{keyword}验证码异常结束')
        driver.quit()
        time.sleep(60)
        return True,True  #True--下一次重新创建实例, True--循环这个词语
    print(f"{keyword}正常结束")
    matchkeyword(html,keyword)
    return False,False  #下次继续用，, True--正常词语，不用循环这个词语


    # # session.close()
    #多线程运行
def ThreadLine():
    print('213')
    # with futures.ThreadPoolExecutor(max_workers=1) as executor:  # 多线程
    #     #for循环进行多线程调用
    #     for i in readtxt():
    #         #在循环内实例化，在外边实例化会让所有的值一致
    #         executor.submit(gethtml,i)


# 思路:1.循环读取match.txt列表，
# 采用多线程请求request，
# 每个线程获取到html 就判断是否有验证，没有的matchkeyword进行读写
if __name__ == '__main__':
    ThreadLine()
    # gethtml('淘券网')

    # 创建锁,true 说明不存在driver ，需要重新创建
    # 第一次锁为True，运行创建driver，
    # 正常运行一次循环，设置锁为False ，不在创建driver
    # 如果遇到验证码，关闭旧driver ，设置锁为True，重新进入if创建driver
    SETCreate = True #改状态为driver不存在状态
    driver=None
    for i in readtxt():
        SETWhile = True
        while SETWhile:
            if SETCreate: #
                option = ChromeOptions()
                option.add_experimental_option('excludeSwitches', ['enable-automation'])
                # option.add_argument('headless')
                driver = Chrome(options=option)
            SETCreate,SETWhile=gethtml(i,driver)

